package com.wushi.lenovo.asia5b.app_ui.profile.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.ListPopupWindow;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.mvp.base.BaseMvpActivity;
import com.mvp.base.model.ResultModel;
import com.mvp.common.GlobalValue;
import com.mvp.utils.ToastUtil;
import com.mvp.utils.https.MapHelper;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_config.UserRepository;
import com.wushi.lenovo.asia5b.app_di.APPComponentFactory;
import com.wushi.lenovo.asia5b.app_di.other.presenter.CodePresenter;
import com.wushi.lenovo.asia5b.app_di.profile.mvpview.ChangeMobileActivityView;
import com.wushi.lenovo.asia5b.app_di.profile.presenter.ChangeMobileActivityPresenter;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;
import com.wushi.lenovo.asia5b.app_model.profile.CodeModel;
import com.wushi.lenovo.asia5b.app_model.profile.CountryModel;
import com.wushi.lenovo.asia5b.app_utils.WToastHelper;
import com.wushi.lenovo.asia5b.app_utils.eye.PopupListAdapter;
import com.wushi.lenovo.asia5b.app_utils.eye.PopupWindowHelper;
import com.wushi.lenovo.asia5b.app_widget.ErrorTextView;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import timber.log.Timber;

/**
 * Created by zhangyuncai on 2018/12/6.
 * 改变手机号
 */
public class ChangeMobileActivity extends BaseMvpActivity<
        ChangeMobileActivityView, ChangeMobileActivityPresenter> implements ChangeMobileActivityView {
    @BindView(R.id.tv_changemobile_country)
    TextView tv_changemobile_country;
    @BindView(R.id.tv_changemobile_areacode)
    TextView tv_changemobile_areacode;
    @BindView(R.id.et_changemobile_mobile)
    EditText et_changemobile_mobile;
    @BindView(R.id.et_changemobile_code)
    EditText et_changemobile_code;
    @BindView(R.id.tv_changemobile_getcode)
    TextView tv_changemobile_getcode;
    @BindView(R.id.bt_changemobile_save)
    Button bt_changemobile_save;
    @BindView(R.id.bt_changemobile_cancle)
    Button bt_changemobile_cancle;
    @BindView(R.id.tv_changemobile_code_error)
    ErrorTextView tv_changemobile_code_error;
    private PopupListAdapter<CountryModel> popupListAdapter;
    private ListPopupWindow popupWindow;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_changemobile;
    }

    @Override
    protected void constructComponent() {
        APPComponentFactory.getAppBaseComponent().inject(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();

        initData();

        initEvent();
    }

    @Override
    protected void initView() {
        initPopupWindow();
        setView();
    }

    private void setView() {
        UserInfo userInfo = UserInfo.getUserInfo();

        //区号
        tv_changemobile_areacode.setText(userInfo.getCalling_code());
        //手机号
        et_changemobile_mobile.setText(userInfo.getMobile());
    }

    private void initPopupWindow() {
        popupListAdapter = new PopupListAdapter<>(this, R.layout.item_popupitem_login_adapter);
        popupWindow = PopupWindowHelper.getListPopupWindow(this, popupListAdapter
                , tv_changemobile_country, ListPopupWindow.WRAP_CONTENT);
    }

    @Override
    protected void initData() {
        getPresenter().getCountrys();
    }

    private void sendCode() {
        String phone = et_changemobile_mobile.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            return;
        }
        CountryModel selectItem = popupListAdapter.getSelectItem();
        if (selectItem == null) {
            return;
        }
        getPresenter().sendCode(phone, selectItem.getCalling_code());
    }

    private void save() {
        String phone = et_changemobile_mobile.getText().toString().trim();
        String code = et_changemobile_code.getText().toString().trim();
        if (TextUtils.isEmpty(code)) {
            tv_changemobile_code_error.setText(R.string.filed_is_empty);
            tv_changemobile_code_error.setVisibility(View.VISIBLE);
            return;
        }
        Object tag = et_changemobile_code.getTag();
        if (tag != null && tag instanceof String) {
            String tagCode = (String) tag;
            if (!TextUtils.equals(code, tagCode)) {
                tv_changemobile_code_error.setText(R.string.invalid_code);
                tv_changemobile_code_error.setVisibility(View.VISIBLE);
                return;
            }
        }
        if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(code)) {
            return;
        }
        Map<String, Object> map = MapHelper.newHashMap();
        map.put("member_id", "" + UserInfo.getMemberId());
        map.put("mobile", phone);
        map.put("otp", code);
        getPresenter().changeMobile(map);
    }

    private void cancel() {
        finish();
    }

    @Override
    protected void initEvent() {
        tv_changemobile_code_error.setBindEditText(et_changemobile_code);
        popupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                popupWindow.dismiss();
                popupListAdapter.setSelectPosition(position);
                CountryModel selectCountryModel = popupListAdapter.getSelectItem();
                if (selectCountryModel != null) {
                    tv_changemobile_country.setText(selectCountryModel.getName());
                    tv_changemobile_areacode.setText("+" + selectCountryModel.getCalling_code());
                }
            }
        });
    }

    @OnClick({
            R.id.bt_changemobile_save,
            R.id.bt_changemobile_cancle,
            R.id.tv_changemobile_getcode,
            R.id.tv_changemobile_country
    })
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_changemobile_save:
                save();
                break;
            case R.id.bt_changemobile_cancle:
                cancel();
                break;
            case R.id.tv_changemobile_getcode:
                et_changemobile_code.setTag("");
                sendCode();
                break;
            case R.id.tv_changemobile_country:
                PopupWindowHelper.showPopupWindow(popupWindow);
                break;
            default:
                break;
        }

    }

    @Override
    public void sendCodeResult(ResultModel<CodeModel> resultModel, CodePresenter.CodeType codeType) {

        if (resultModel != null && resultModel.isSuccess()) {
            ToastUtil.showToast(getString(R.string.otp_send_success));
            getPresenter().interval();

            CodeModel codeModel = resultModel.getData();
            if (codeModel != null) {
                et_changemobile_code.setTag(codeModel.getOTP_code());
                if (GlobalValue.isDebug) {
                    et_changemobile_code.setText(codeModel.getOTP_code());
                }
            }
        }
    }

    @Override
    public void intervalResult(long aLong) {
        Timber.d("--->>>>>>>>---aLong:" + aLong);
        if (aLong == 0) {
            tv_changemobile_getcode.setEnabled(true);
            tv_changemobile_getcode.setText(R.string.chongxinhuoquyanzhengma);
        } else {
            tv_changemobile_getcode.setEnabled(false);
            tv_changemobile_getcode.setText(aLong + "s");
        }
    }

    @Override
    public void getCountrysResult(List<CountryModel> countryModels) {
        List<CountryModel> models = countryModels;
        popupListAdapter.setDatas(models);
        if (models != null) {
            for (int i = 0; i < models.size(); i++) {
                CountryModel countryModel = models.get(i);
                if (countryModel.getId() == UserInfo.getUserInfo().getCountry_id()) {
                    popupListAdapter.setSelectPosition(i);
                    tv_changemobile_country.setText(countryModel.getName());
                    break;
                }
            }
        }
    }

    @Override
    public void changeMobileResult(ResultModel resultModel, Map<String, Object> map) {
        if (resultModel.isSuccess()) {
            WToastHelper.showContentView(this, R.string.changemobile_success);
            UserRepository.updateUserInfo();
            startActivity(new Intent(mActivity, ProfileActivity.class));
            finish();
        }
    }
}
